package com.zhuangjie.mvc.ws.config;

import com.zhuangjie.mvc.ws.core.WebSocketDispatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import java.util.Set;

// 添加 @EnableWebSocket 启用
@Configuration
@EnableWebSocket
public class WebSocketAutoConfiguration implements WebSocketConfigurer {
    @Autowired
    private WebSocketDispatch webSocketDispatch;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        Set<String> wsRegisterData = webSocketDispatch.initAndGetDispatchHandlerRegisterInfo();
        for (String registerPath : wsRegisterData) {
            registerPath = ("/"+registerPath+"/**").replaceAll("//+", "/");
            registry.addHandler(webSocketDispatch,registerPath).setAllowedOrigins("*");
        }
    }
}
